﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using DBUtility;
using MyExection;
using System.Data.SqlClient;
using Model.Security.EntityInfo;
using Util;
using Model.BusinessLogic.EntityInfo;

namespace BusinessLogicDAL
{
    /// <summary>
    /// 功能介绍：联系人数据访问层
    /// </summary>
    public class LianXiRenDAL
    {
        private readonly string conStr = ConfigurationManager.AppSettings["ERPDataConnStr"];

        public LianXiRenDAL()
        {
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能介绍：添加LianXiRenEntity
        /// </summary>
        /// <param name="entityInfo"></param>
        /// <returns></returns>
        public int Add(SqlTransaction trans, LianXiRenEntity entityInfo)
        {
            string strSql = GetAddSQLStrings();
            SqlParameter[] sqlParas = GetLianXiRenEntityAddParas(entityInfo);
            try
            {
                return DataSwitch.ConvertObjectToInt32(DBSQLHelper.ExecuteScalar(trans, CommandType.Text, strSql, sqlParas));
            }
            catch (Exception e)
            {
                throw new CustomException(e.Message, "LianXiRenDAO.Add(LianXiRenEntity entityInfo)", "根据实体添加数据");
            }
        }
        /// <summary>
        /// 创建人：
        /// 创建时间:2010-7-8
        /// 功能简介:返回Sql语句
        /// </summary>
        /// <returns></returns>
        private string GetAddSQLStrings()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO TB_LianXiRen( CustomerID,[Name],Title,Position,Address,Tel1,Tel2,MobilePhone,Fax,Email,Gender,Profession) values( @CustomerID,@Name,@Title,@Position,@Address,@Tel1,@Tel2,@MobilePhone,@Fax,@Email,@Gender,@Profession)");
            strSql.Append("	Select scope_identity()");
            return strSql.ToString();
        }

        /// <summary>
        /// 创建人：
        /// 创建时间:2010-7-8
        /// 功能简介:返回LianXiRenEntity参数赋值组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private SqlParameter[] GetLianXiRenEntityAddParas(LianXiRenEntity entity)
        {
            SqlParameter[] parameter = new SqlParameter[]
                {
                    new SqlParameter("@CID",entity.CID),new SqlParameter("@CustomerID",entity.CustomerID),new SqlParameter("@Name",entity.Name),new SqlParameter("@Title",entity.Title),new SqlParameter("@Position",entity.Position),new SqlParameter("@Address",entity.Address),new SqlParameter("@Tel1",entity.Tel1),new SqlParameter("@Tel2",entity.Tel2),new SqlParameter("@MobilePhone",entity.MobilePhone),new SqlParameter("@Fax",entity.Fax),new SqlParameter("@Email",entity.Email),new SqlParameter("@Gender",entity.Gender),new SqlParameter("@Profession",entity.Profession)
               
                 };
            return parameter;
        }


        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能介绍：删除LianXiRenEntity
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Delete(SqlTransaction trans, string key)
        {
            string strSql = GetDeleteSQLStrings();
            SqlParameter[] sqlParas = GetDeleteEntityParas(key);
            try
            {
                DBSQLHelper.ExecuteNonQuery(trans, CommandType.Text, strSql, sqlParas);
                return true;
            }
            catch (Exception e)
            {
                throw new CustomException(e.Message, "LianXiRenDAL.Delete(key)", "根据条件删除LianXiRenEntity信息，同时返回删除结果");
            }
        }

        /// <summary>
        /// 创建人：
        /// 创建时间:2010-7-8
        /// 功能简介:返回Sql语句
        /// </summary>
        /// <returns></returns>
        private string GetDeleteSQLStrings()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TB_LianXiRen WHERE CustomerID= @CustomerID");
            return strSql.ToString();
        }

        /// <summary>
        /// 创建人：
        /// 创建时间:2010-7-8
        /// 功能简介:返回DeleteEntity参数赋值组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private SqlParameter[] GetDeleteEntityParas(string key)
        {
            SqlParameter[] parameter = new SqlParameter[]
                {
                    new SqlParameter("@CustomerID",key)
               
                 };
            return parameter;
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能介绍：更新LianXiRenEntity
        /// </summary> 
        /// <param name="entityInfo"></param>
        /// <returns></returns>      
        public bool Update(LianXiRenEntity entityInfo)
        {
            string strSql = GetUpdateSQLStrings();
            SqlParameter[] sqlParas = GetUpdateLianXiRenEntity(entityInfo);
            try
            {
                int result = DBSQLHelper.ExecuteNonQuery(conStr, CommandType.Text, strSql, sqlParas);
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw new CustomException(e.Message, "LianXiRenDAL.Update(LianXiRenEntity entityInfo)", "更新实体信息");
            }
        }
        /// <summary>
        /// 创建人：
        /// 创建时间:2010-7-8
        /// 功能简介:返回Sql语句
        /// </summary>
        /// <returns></returns>
        private string GetUpdateSQLStrings()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TB_LianXiRen SET  CustomerID=@CustomerID,[Name]=@Name,Title=@Title,Position=@Position,Address=@Address,Tel1=@Tel1,Tel2=@Tel2,MobilePhone=@MobilePhone,Fax=@Fax,Email=@Email,Gender=@Gender,Profession=@Profession WHERE  1 = 1 AND CID= @CID");
            return strSql.ToString();
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能简介：返回LianXiRenEntity参数赋值
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private SqlParameter[] GetUpdateLianXiRenEntity(LianXiRenEntity entity)
        {
            SqlParameter[] parameter = new SqlParameter[]
                {
                    new SqlParameter("@CID",entity.CID),new SqlParameter("@CustomerID",entity.CustomerID),new SqlParameter("@Name",entity.Name),new SqlParameter("@Title",entity.Title),new SqlParameter("@Position",entity.Position),new SqlParameter("@Address",entity.Address),new SqlParameter("@Tel1",entity.Tel1),new SqlParameter("@Tel2",entity.Tel2),new SqlParameter("@MobilePhone",entity.MobilePhone),new SqlParameter("@Fax",entity.Fax),new SqlParameter("@Email",entity.Email),new SqlParameter("@Gender",entity.Gender),new SqlParameter("@Profession",entity.Profession)
               
                 };
            return parameter;
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能介绍：根据“key”查询LianXiRenEntity
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public LianXiRenEntity GetListDetailById(string key)
        {
            DataRow row = null;
            LianXiRenEntity lianXiRenEntity = new LianXiRenEntity();
            StringBuilder sbSqlBuilder = new StringBuilder();
            try
            {
                sbSqlBuilder.Append("SELECT * FROM TB_LianXiRen WHERE TB_LianXiRen.CID = @CID");
                string sqlStr = sbSqlBuilder.ToString();
                SqlParameter[] parameters = new SqlParameter[]{
                        new SqlParameter("@CID",key)
                    };
                DataTable LianXiRenDT = new DataTable();
                LianXiRenDT = DBSQLHelper.GetDataTable(conStr, CommandType.Text, sqlStr, parameters);
                if ((LianXiRenDT != null) && (LianXiRenDT.Rows.Count > 0))
                {
                    row = LianXiRenDT.Rows[0];
                }
                else
                {
                    return lianXiRenEntity;
                }
            }
            catch (Exception e)
            {
                throw new CustomException(e.Message, "LianXiRenDAL.GetListDetailById(string key)", "获得LianXiRen详细信息");
            }
            lianXiRenEntity.CID = DataSwitch.ConvertObjectToInt32(row["CID"]);
            lianXiRenEntity.CustomerID = DataSwitch.ConvertObjectToInt32(row["CustomerID"]);
            lianXiRenEntity.Name = DataSwitch.ConvertObjectToString(row["Name"]);
            lianXiRenEntity.Title = DataSwitch.ConvertObjectToString(row["Title"]);
            lianXiRenEntity.Position = DataSwitch.ConvertObjectToString(row["Position"]);
            lianXiRenEntity.Address = DataSwitch.ConvertObjectToString(row["Address"]);
            lianXiRenEntity.Tel1 = DataSwitch.ConvertObjectToString(row["Tel1"]);
            lianXiRenEntity.Tel2 = DataSwitch.ConvertObjectToString(row["Tel2"]);
            lianXiRenEntity.MobilePhone = DataSwitch.ConvertObjectToString(row["MobilePhone"]);
            lianXiRenEntity.Fax = DataSwitch.ConvertObjectToString(row["Fax"]);
            lianXiRenEntity.Email = DataSwitch.ConvertObjectToString(row["Email"]);
            lianXiRenEntity.Gender = DataSwitch.ConvertObjectToString(row["Gender"]);
            lianXiRenEntity.Profession = DataSwitch.ConvertObjectToString(row["Profession"]);

            return lianXiRenEntity;

        }
        /// <summary>
        /// 获得LianXiRen DataTable
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public DataTable GetLianXiRenDTByCustomerID(int CustomerID)
        {
            try
            {
                StringBuilder sbSqlBuilder = new StringBuilder();
                sbSqlBuilder.Append("SELECT * FROM TB_LianXiRen WHERE TB_LianXiRen.CustomerID = @CustomerID");
                string sqlStr = sbSqlBuilder.ToString();
                SqlParameter[] parameters = new SqlParameter[]{
                        new SqlParameter("@CustomerID",CustomerID)
                    };
                return DBSQLHelper.GetDataTable(conStr, CommandType.Text, sqlStr, parameters);
            }
            catch (Exception e)
            {
                throw new CustomException(e.Message, "LianXiRenDAL.GetLianXiRenDTByCustomerID(int CustomerID)", "获得LianXiRen DataTable");
            }
        }
    }
}
